package com.fr.web.core;

import com.fr.base.FRContext;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.file.ConfigManager;
import com.fr.report.FineBook;
import com.fr.report.PageSetChain;
import com.fr.report.ReportWebAttr;
import com.fr.report.ResultWorkBook;
import com.fr.report.TemplateWorkBook;
import com.fr.report.core.ReportUtils;
import com.fr.report.web.ToolBarManager;
import com.fr.report.web.WebForm;
import com.fr.report.web.WebPage;
import com.fr.report.web.WebWrite;
import com.fr.report.web.event.Listener;
import com.fr.web.EmbeddedResultResultlet;
import com.fr.web.EmbeddedTplReportlet;
import com.fr.web.Reportlet;
import com.fr.web.Repository;
import com.fr.web.Resultlet;
import java.io.Serializable;
import java.util.Map;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;

public abstract class ShowWorkBookPolicy
  implements Serializable, Cloneable
{
  public static final String PANEL_TYPE_PAGE = "page";
  public static final String PANEL_TYPE_VIEW = "view";
  public static final String PANEL_TYPE_FORM = "form";
  public static final String PANEL_TYPE_SHEET = "sheet";
  public static final String PANEL_TYPE_WRITE = "write";
  public static final String PANEL_TYPE_EDIT = "edit";
  public static final String PANEL_TYPE_PREVIEW = "preview";

  public abstract ResultWorkBook show(TemplateWorkBook paramTemplateWorkBook, Map paramMap);

  public abstract String panelType();

  public PageSetChain getPageSet(ResultWorkBook paramResultWorkBook)
  {
    return ((paramResultWorkBook == null) ? null : paramResultWorkBook.generateReportPageSet(ReportUtils.getPaperSettingListFromWorkBook(paramResultWorkBook)));
  }

  public JSONObject panelConfig(Repository paramRepository)
    throws JSONException
  {
    return new JSONObject();
  }

  public abstract ToolBarManager[] toolbarManagers(Repository paramRepository);

  public static ShowWorkBookPolicy getInstance(HttpServletRequest paramHttpServletRequest, Reportlet paramReportlet)
  {
    if (paramReportlet instanceof EmbeddedTplReportlet)
      return ((EmbeddedTplReportlet)paramReportlet).getPolicy();
    String str = paramHttpServletRequest.getParameter("op");
    Object localObject = getCommonPolicy(str);
    if (localObject == null)
      if ("write".equals(str))
        localObject = new Policy4Write();
      else if (("form".equals(str)) || ("sheet".equals(str)))
        localObject = new Policy4Form();
      else
        localObject = new Policy4Page();
    return ((ShowWorkBookPolicy)localObject);
  }

  public static ShowWorkBookPolicy getInstance(HttpServletRequest paramHttpServletRequest, Resultlet paramResultlet)
  {
    if (paramResultlet instanceof EmbeddedResultResultlet)
      return ((EmbeddedResultResultlet)paramResultlet).getPolicy();
    String str = paramHttpServletRequest.getParameter("op");
    Object localObject = getCommonPolicy(str);
    if (localObject == null)
      localObject = new Policy4Page();
    return ((ShowWorkBookPolicy)localObject);
  }

  private static ShowWorkBookPolicy getCommonPolicy(String paramString)
  {
    if (("view".equals(paramString)) || ("edit".equals(paramString)))
      return new Policy4View();
    if ("simplesheet".equals(paramString))
      return new Policy4SimpleSheet();
    return null;
  }

  public static final class Policy4Write extends ShowWorkBookPolicy
  {
    public ResultWorkBook show(TemplateWorkBook paramTemplateWorkBook, Map paramMap)
    {
      return paramTemplateWorkBook.execute4Write(paramMap);
    }

    public String panelType()
    {
      return "write";
    }

    public JSONObject panelConfig(Repository paramRepository)
      throws JSONException
    {
      JSONObject localJSONObject = super.panelConfig(paramRepository);
      SessionIDInfor localSessionIDInfor = paramRepository.getSessionIDInfor();
      String[] arrayOfString = ReportUtils.getWriteImportedCssFromWorkBook(localSessionIDInfor.getContextBook());
      if (arrayOfString.length > 0)
        localJSONObject.put("writeCss", arrayOfString);
      ReportWebAttr localReportWebAttr = localSessionIDInfor.getContextBook().getReportWebAttr();
      if (localReportWebAttr == null)
        localReportWebAttr = FRContext.getConfigManager().getReportWebAttr();
      if ((localReportWebAttr != null) && (localReportWebAttr.getWebWrite() != null))
      {
        WebWrite localWebWrite = localReportWebAttr.getWebWrite();
        JSONArray localJSONArray = null;
        for (int i = 0; i < localWebWrite.getListenerSize(); ++i)
        {
          Listener localListener = localWebWrite.getListener(i);
          if (localJSONArray == null)
            localJSONArray = new JSONArray();
          localJSONArray.put(localListener.createJSONConfig(paramRepository));
        }
        if (localJSONArray != null)
          localJSONObject.put("listeners", localJSONArray);
        if (localWebWrite.getSelectedColor() != null)
          localJSONObject.put("selectedColor", localWebWrite.getColor());
      }
      return localJSONObject;
    }

    public ToolBarManager[] toolbarManagers(Repository paramRepository)
    {
      ToolBarManager[] arrayOfToolBarManager = ReportUtils.getToolBarFromWorkBook(paramRepository.getSessionIDInfor().getContextBook(), 2);
      if (arrayOfToolBarManager == null)
      {
        ReportWebAttr localReportWebAttr = FRContext.getConfigManager().getReportWebAttr();
        if (localReportWebAttr != null)
        {
          WebWrite localWebWrite = localReportWebAttr.getWebWrite();
          if (localWebWrite != null)
            arrayOfToolBarManager = localWebWrite.getToolBarManagers();
        }
      }
      if (arrayOfToolBarManager == null)
        arrayOfToolBarManager = new ToolBarManager[] { ToolBarManager.createDefaultWriteToolBar() };
      return arrayOfToolBarManager;
    }

    public String toString()
    {
      return "execute4write";
    }
  }

  public static final class Policy4SimpleSheet extends ShowWorkBookPolicy.Policy4Form
  {
    public String panelType()
    {
      return "simplesheet";
    }

    public String toString()
    {
      return "execute4simplesheet";
    }
  }

  public static class Policy4Form extends ShowWorkBookPolicy
  {
    public ResultWorkBook show(TemplateWorkBook paramTemplateWorkBook, Map paramMap)
    {
      return paramTemplateWorkBook.execute4Form(paramMap);
    }

    public ToolBarManager[] toolbarManagers(Repository paramRepository)
    {
      return new ToolBarManager[0];
    }

    public String panelType()
    {
      return "form";
    }

    public JSONObject panelConfig(Repository paramRepository)
    {
      JSONObject localJSONObject = null;
      try
      {
        localJSONObject = super.panelConfig(paramRepository);
      }
      catch (JSONException localJSONException1)
      {
        FRContext.getLogger().warning(localJSONException1.getMessage());
      }
      SessionIDInfor localSessionIDInfor = paramRepository.getSessionIDInfor();
      ReportWebAttr localReportWebAttr = localSessionIDInfor.getContextBook().getReportWebAttr();
      if (localReportWebAttr == null)
        localReportWebAttr = FRContext.getConfigManager().getReportWebAttr();
      if ((localReportWebAttr != null) && (localReportWebAttr.getWebForm() != null))
      {
        WebForm localWebForm = localReportWebAttr.getWebForm();
        JSONArray localJSONArray = null;
        for (int i = 0; i < localWebForm.getListenerSize(); ++i)
        {
          Listener localListener = localWebForm.getListener(i);
          try
          {
            if (localJSONArray == null)
              localJSONArray = new JSONArray();
            localJSONArray.put(localListener.createJSONConfig(paramRepository));
          }
          catch (JSONException localJSONException3)
          {
            FRContext.getLogger().warning(localJSONException3.getMessage());
          }
        }
        if (localJSONArray != null)
          try
          {
            localJSONObject.put("listeners", localJSONArray);
          }
          catch (JSONException localJSONException2)
          {
            FRContext.getLogger().warning(localJSONException2.getMessage());
          }
      }
      return localJSONObject;
    }

    public String toString()
    {
      return "execute4form";
    }
  }

  public static final class Policy4View extends ShowWorkBookPolicy.Policy4Page
  {
    public String panelType()
    {
      return "view";
    }

    protected ToolBarManager[] getDefineToolBars(Repository paramRepository)
    {
      return ReportUtils.getToolBarFromWorkBook(paramRepository.getSessionIDInfor().getContextBook(), 3);
    }

    public PageSetChain getPageSet(ResultWorkBook paramResultWorkBook)
    {
      return ((paramResultWorkBook == null) ? null : paramResultWorkBook.generateReportPageSet(null));
    }

    public String toString()
    {
      return "execute4view";
    }
  }

  public static class Policy4Page extends ShowWorkBookPolicy
  {
    public ResultWorkBook show(TemplateWorkBook paramTemplateWorkBook, Map paramMap)
    {
      return paramTemplateWorkBook.execute(paramMap);
    }

    public ToolBarManager[] toolbarManagers(Repository paramRepository)
    {
      ToolBarManager[] arrayOfToolBarManager = getDefineToolBars(paramRepository);
      if (arrayOfToolBarManager == null)
      {
        ReportWebAttr localReportWebAttr = FRContext.getConfigManager().getReportWebAttr();
        if (localReportWebAttr != null)
        {
          WebPage localWebPage = localReportWebAttr.getWebPage();
          if (localWebPage != null)
            arrayOfToolBarManager = localWebPage.getToolBarManagers();
        }
      }
      if (arrayOfToolBarManager == null)
        arrayOfToolBarManager = new ToolBarManager[] { ToolBarManager.createDefaultToolBar() };
      return arrayOfToolBarManager;
    }

    protected ToolBarManager[] getDefineToolBars(Repository paramRepository)
    {
      return ReportUtils.getToolBarFromWorkBook(paramRepository.getSessionIDInfor().getContextBook(), 0);
    }

    public String panelType()
    {
      return "page";
    }

    public JSONObject panelConfig(Repository paramRepository)
    {
      JSONObject localJSONObject = null;
      try
      {
        localJSONObject = super.panelConfig(paramRepository);
      }
      catch (JSONException localJSONException1)
      {
        FRContext.getLogger().warning(localJSONException1.getMessage());
      }
      SessionIDInfor localSessionIDInfor = paramRepository.getSessionIDInfor();
      ReportWebAttr localReportWebAttr = localSessionIDInfor.getContextBook().getReportWebAttr();
      if (localReportWebAttr == null)
        localReportWebAttr = FRContext.getConfigManager().getReportWebAttr();
      if ((localReportWebAttr != null) && (localReportWebAttr.getWebPage() != null))
      {
        WebPage localWebPage = localReportWebAttr.getWebPage();
        JSONArray localJSONArray = null;
        for (int i = 0; i < localWebPage.getListenerSize(); ++i)
        {
          Listener localListener = localWebPage.getListener(i);
          try
          {
            if (localJSONArray == null)
              localJSONArray = new JSONArray();
            localJSONArray.put(localListener.createJSONConfig(paramRepository));
          }
          catch (JSONException localJSONException3)
          {
            FRContext.getLogger().warning(localJSONException3.getMessage());
          }
        }
        if (localJSONArray != null)
          try
          {
            localJSONObject.put("listeners", localJSONArray);
          }
          catch (JSONException localJSONException2)
          {
            FRContext.getLogger().warning(localJSONException2.getMessage());
          }
      }
      return localJSONObject;
    }

    public String toString()
    {
      return "execute";
    }
  }
}